
import pymysql

# 获取数据库连接
# 参数1, ip
# 参数2: 数据库用户名
# 参数3: 数据库用户密码
# 参数4: 数据库名
db = pymysql.connect("localhost", "root", "123456", "ssm")

# 创建一个数据库连接对象
cursor = db.cursor()

sql = "select version()"

# 执行SQL语句
cursor.execute(sql)

# 获取返回信息(单条)
data = cursor.fetchone()

print("查询:", data)

sql2 = "select * from  tb_mv"

cursor.execute(sql2)

data2 = cursor.fetchall()

print("查询列表:", data2)

index = 1
for row in data2:
    print(index, "-", row[1], "-", row[2])
    index += 1

# 断开
cursor.close()
db.close()

